package mi.payment.transaction.revoke;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import mi.payment.R;
import mi.payment.adapter.BaseListAdapter;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.TransactionOtherOperation;
import mi.payment.transaction.authorize.complete.revoke.PreAuthorizeCompleteRevokeInputAdminPwdActivity;
import mi.payment.transaction.authorize.complete.revoke.PreAuthorizeCompleteRevokeSwingActivity;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.NetUtils;
import mi.payment.utils.SystemDateTime;

/**
 * 消费撤销模块 -- 适配器
 */
public class RevokeAdapter extends BaseListAdapter<PayDetail> {

    private int redColor;
    private int sixColor;
    private int nineColor;
    private int dfColor;

    public RevokeAdapter(Context context) {
        super(context);
        redColor = Color.parseColor("#fd5a52");
        sixColor = Color.parseColor("#666666");
        nineColor = Color.parseColor("#999999");
        dfColor = Color.parseColor("#daa8a6");
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.transaction_revoke_item_lv, null);
            holder.tvDate = convertView.findViewById(R.id.tv_date);
            holder.tvTime = convertView.findViewById(R.id.tv_time);
            holder.viewTop = convertView.findViewById(R.id.time_top);
            holder.viewRound = convertView.findViewById(R.id.time_round);
            holder.viewBottom = convertView.findViewById(R.id.time_bottom);
            holder.tvAmount = convertView.findViewById(R.id.tv_amount);
            holder.tvMerchant = convertView.findViewById(R.id.tv_merchant);
            holder.tvRevoke = convertView.findViewById(R.id.tv_revoke);
            holder.content = convertView.findViewById(R.id.view_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final PayDetail item = getItem(position);
        if (item.TradeDate != null && item.TradeDate.length() >= 4) {
            String date = item.TradeDate.substring(0, 2) + "/" + item.TradeDate.substring(2, 4);
            String time = item.TradeTime.substring(0, 2) + ":" + item.TradeTime.substring(2, 4);
            holder.tvDate.setText(date);
            holder.tvTime.setText(time);
        } else {
            String date = SystemDateTime.getDateByStamp(item.TerminalDate);
            String time = SystemDateTime.getTimeByStamp(item.TerminalDate);
            holder.tvDate.setText(date);
            holder.tvTime.setText(time);
        }

        if (position == 0) {
            // 如果是第一条数据，不显示上面的时间轴
            // holder.viewTop.setVisibility(View.INVISIBLE);
        } else {
            // holder.viewTop.setVisibility(View.VISIBLE);
        }
        String text = getString(R.string.money_only_symbol) + MoneyUtils.longCent2DoubleMoneyStr(item.amount);
        holder.tvAmount.setText(text);

        String voucherNo;
        if (item.transPlatform == 0) {
            voucherNo = getString(R.string.voucher_number) + " " + item.voucherNo;
            Drawable img = getDrawable(R.drawable.item_bank_card);
            int minimumWidth = img.getMinimumWidth();
            int minimumHeight = img.getMinimumHeight();
            img.setBounds(0, 0, minimumWidth, minimumHeight);
            holder.tvAmount.setCompoundDrawables(img, null, null, null);
        } else if (item.transPlatform == 1) {
            voucherNo = getString(R.string.third_order_number) + " " + item.thirdTransNum;
            Drawable img = getDrawable(R.drawable.item_ali_code);
            int minimumWidth = img.getMinimumWidth();
            int minimumHeight = img.getMinimumHeight();
            img.setBounds(0, 0, minimumWidth, minimumHeight);
            holder.tvAmount.setCompoundDrawables(img, null, null, null);
        } else if (item.transPlatform == 2) {
            voucherNo = getString(R.string.third_order_number) + " " + item.thirdTransNum;
            Drawable img = getDrawable(R.drawable.item_wx_code);
            int minimumWidth = img.getMinimumWidth();
            int minimumHeight = img.getMinimumHeight();
            img.setBounds(0, 0, minimumWidth, minimumHeight);
            holder.tvAmount.setCompoundDrawables(img, null, null, null);
        } else if (item.transPlatform == 4) {
            voucherNo = getString(R.string.third_order_number) + " " + item.thirdTransNum;
            Drawable img = getDrawable(R.drawable.item_union_code);
            int minimumWidth = img.getMinimumWidth();
            int minimumHeight = img.getMinimumHeight();
            img.setBounds(0, 0, minimumWidth, minimumHeight);
            holder.tvAmount.setCompoundDrawables(img, null, null, null);
        } else {
            voucherNo = getString(R.string.third_order_number) + " " + item.thirdTransNum;
            Drawable img = getDrawable(R.drawable.item_mi_code);
            int minimumWidth = img.getMinimumWidth();
            int minimumHeight = img.getMinimumHeight();
            img.setBounds(0, 0, minimumWidth, minimumHeight);
            holder.tvAmount.setCompoundDrawables(img, null, null, null);
        }

        holder.tvMerchant.setText(voucherNo);
        holder.tvRevoke.setVisibility(View.GONE);
        holder.content.setTag(position);

        if (item.isCanceled) {
            holder.tvAmount.setTextColor(nineColor);
            holder.tvMerchant.setTextColor(nineColor);
            holder.tvRevoke.setTextColor(dfColor);
            holder.tvRevoke.setVisibility(View.VISIBLE);
            holder.tvRevoke.setText(R.string.revoked);
            holder.viewRound.setBackgroundResource(R.drawable.bg_gray_round);
            holder.content.setBackgroundResource(R.drawable.ic_transaction_press_bg);
            holder.content.setOnClickListener(null);
        } else {
            holder.tvAmount.setTextColor(sixColor);
            holder.tvMerchant.setTextColor(sixColor);
            holder.tvRevoke.setTextColor(redColor);
            holder.tvRevoke.setText(R.string.revoke);
            holder.tvRevoke.setVisibility(View.VISIBLE);
            holder.viewRound.setBackgroundResource(R.drawable.bg_red_round);
            holder.content.setBackgroundResource(R.drawable.ic_transaction_bg);
            holder.content.setOnClickListener(this::gotoInputAdminPwd);
        }

        return convertView;
    }

    private void gotoInputAdminPwd(View v) {
        boolean networkUsable = NetUtils.isNetworkUsable(mContext);
        if (networkUsable) {
            int position = (int) v.getTag();
            PayDetail pay = getItem(position);
            if (pay.getTransPlatform() > 0) {
                voidCode(pay);
            } else {
                voidCard(pay);
            }
        } else {
            Toast.makeText(mContext, R.string.ui_hint_network, Toast.LENGTH_SHORT).show();
        }
    }

    private void voidCode(PayDetail payDetail) {
        RevokeCodeInputAdminPwdActivity.startAction(mContext, payDetail);
    }

    private void voidCard(PayDetail payDetail) {
        boolean pwd = TransactionOtherOperation.getInstance().getTransactionOtherSettings().getInputDirectorPsd();
        if (pwd) { // 是否输入主管密码
            if (payDetail.getTransactionType() == 1) {
                RevokeInputAdminPwdActivity.startAction(mContext, payDetail);
            } else if (payDetail.getTransactionType() == 6) {
                PreAuthorizeCompleteRevokeInputAdminPwdActivity.startAction(mContext, payDetail);
            }
        } else {
            if (payDetail.getTransactionType() == 1) {
                RevokeSwingCardActivity.startAction(mContext, payDetail);
            } else if (payDetail.getTransactionType() == 6) {
                PreAuthorizeCompleteRevokeSwingActivity.startAction(mContext, payDetail);
            }
        }
    }

    private String getString(int resId) {
        return getContext().getString(resId);
    }

    private Drawable getDrawable(int resId) {
        return getContext().getResources().getDrawable(resId);
    }

    private static class ViewHolder {
        private TextView tvDate;
        private TextView tvTime;
        private View viewTop;
        private View viewRound;
        private View viewBottom;
        private TextView tvAmount;
        private TextView tvMerchant;
        private TextView tvRevoke;
        private View content;
    }


}
